package com.andan.recharge.recharge;

import com.avaje.ebean.FutureList;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.entity.Player;

import java.util.concurrent.ExecutionException;

public class hook extends PlaceholderExpansion {
    @Override
    public String getIdentifier() {
        return "recharge";
    }

    @Override
    public String getAuthor() {
        return null;
    }

    @Override
    public String getVersion() {
        return null;
    }

    @Override
    public boolean persist(){
        return true;
    }

    @Override
    public String onPlaceholderRequest(Player p, String params) {
//        %recharge_group%
//        System.out.println(params);
        String group = params;
        String uuid = p.getUniqueId().toString();
        if (!group.equals("ALL")){
            return find_id_num(uuid,group);
        }else {
            return calcNum(uuid);
        }

    }

    private String calcNum(String uuid) {
        FutureList<rechargeDao> player_uuid = Recharge.getDataSource().find(rechargeDao.class)
                .where()
                .eq("player_uuid", uuid)
                .findFutureList();
        int num = 0;
        try {
            for (rechargeDao r : player_uuid.get()){
                num+=r.getData();

            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return ""+num;
    }

    private String find_id_num(String uuid,String group) {
        rechargeDao unique = Recharge.getDataSource().find(rechargeDao.class)
                .where()
                .eq("player_uuid", uuid)
                .eq("group_name", group)
                .findUnique();
        if (unique==null){
            return "0";
        }
        return ""+unique.getData();
    }
}
